package com.rqf.mr.service.impl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import oracle.jdbc.OracleTypes;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.rqf.mr.service.IDrugService;

@Service("DrugService")
public class DrugService implements IDrugService {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	private SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Override
	public List<Map<String, Object>> getDrugPlanListFromEhisView(String inpatiId) {
		if (StringUtils.isEmpty(inpatiId)) {
			return null;
		}
		String sql = "select * from view_ehis_pqzx dd where dd.INPATIID='" + inpatiId + "'";
		List<Map<String, Object>> notExecuteList = jdbcTemplate.queryForList(sql);
		List<Map<String, Object>> executedList = getDrugPlanListFromTabDrugPlan(inpatiId);
		
		String todaySql = "select * from DRUGPLAN dd where dd.INPATIID='" + inpatiId 
				+ "' AND trunc(SUBMITTIME) = trunc(sysdate) ORDER BY SUBMITTIME DESC";
		List<Map<String, Object>> todayList = jdbcTemplate.queryForList(todaySql);
		
		String weekSql = "select * from DRUGPLAN dd where dd.INPATIID='" + inpatiId 
				+ "' AND SUBMITTIME > trunc(sysdate) - 6 AND SUBMITTIME <= trunc(sysdate) + 1 ORDER BY SUBMITTIME DESC";
		List<Map<String, Object>> weekList = jdbcTemplate.queryForList(weekSql);
		
		for (int i = 0; i < notExecuteList.size(); i++) {
			String freqId = notExecuteList.get(i).get("FREQID").toString();
			String notOrdId = notExecuteList.get(i).get("ORDID").toString();
			
			notExecuteList.get(i).put("lastTime", "未执行");
			notExecuteList.get(i).put("sumExecute", "0");   
			
			//仅仅执行一次的医嘱
			//临时医嘱    
			if ( freqId.equals("st") || 
					//今晚  // 明晚  //明晨  
				freqId.equals("BN") || freqId.equals("CN") || freqId.equals("CM") ) {
				for (int j = 0; j < executedList.size(); j++) {
					if ( notExecuteList.get(i).get("ORDID").equals(executedList.get(j).get("ORDID")) ) {
						notExecuteList.remove(i);
						executedList.remove(j);
						i--;
						break;
					}
				}
			}
			
			//持续医嘱   （不用管执行次数）
			//饭前   // 饭后      //长期备用医嘱
			else if ( freqId.equals("AC") || freqId.equals("PC") || freqId.equals("prn")
				//临时备用   //空腹时   //腹泻时
			  || freqId.equals("sos") || freqId.equals("JEJ") || freqId.equals("ALVLA")
			  //便秘时    //疼痛时     //剧痛时
			  || freqId.equals("ALVAD") || freqId.equals("DOLIT") || freqId.equals("DOLUR")
			  //持续
			  || freqId.equals("CX")) {
				//统计 执行次数  最后一次执行时间
				int executeCount = 0;
				int lastExecute = 0;
				for (int j = 0; j < executedList.size(); j++) {
					String todayOrdId = executedList.get(j).get("ORDID").toString();
					if ( todayOrdId.equals(notOrdId) ) {
						executeCount++;
						if ( executeCount==1 ) {
							lastExecute = j;
						}
					}
				}
				notExecuteList.get(i).put("sumExecute", executeCount);
				if ( executeCount==0 ) {
					notExecuteList.get(i).put("lastTime", "未执行");
				}else {
					notExecuteList.get(i).put("lastTime", todayList.get(lastExecute).get("SUBMITTIME"));
				}
			}
			
			//一天执行一次
			//午餐前   // 晚餐前  // 每天一次
			else if ( freqId.equals("APRAN") || freqId.equals("ACOEN") || freqId.equals("qn") || 
					//睡前 //早餐前  //每天1次
					  freqId.equals("HS") || freqId.equals("AJ") || freqId.equals("qd") ) {
				for (int j = 0; j < todayList.size(); j++) {
					String todayOrdId = todayList.get(j).get("ORDID").toString();
					if ( todayOrdId.equals(notOrdId) ) {
						notExecuteList.remove(i);
						todayList.remove(j);
						i--;
						break;
					}
				}
			}
			
			//一天执行两次
			//每天两次   12小时一次
			else if ( freqId.equals("bid") || freqId.equals("q12h") ) {
				int executeCount = 0;
				int lastExecute = 0;
				for (int j = 0; j < todayList.size(); j++) {
					String todayOrdId = todayList.get(j).get("ORDID").toString();
					if ( todayOrdId.equals(notOrdId) ) {
						executeCount++;
						if ( executeCount==1 ) {
							lastExecute = j;
						}
						if ( executeCount>=2 ) {
							break;
						}
					}
				}
				notExecuteList.get(i).put("sumExecute", executeCount);
				if ( executeCount==2 ) {
					notExecuteList.remove(i);
					i--;
				}else if ( executeCount==0 ) {
					notExecuteList.get(i).put("lastTime", "未执行");
				}else {
					notExecuteList.get(i).put("lastTime", todayList.get(lastExecute).get("SUBMITTIME"));
				}
			}
			
			//每天3次   8小时一次
			else if ( freqId.equals("tid") || freqId.equals("q8h") ) {
				int executeCount = 0;
				int lastExecute = 0;
				for (int j = 0; j < todayList.size(); j++) {
					String todayOrdId = todayList.get(j).get("ORDID").toString();
					if ( todayOrdId.equals(notOrdId) ) {
						executeCount++;
						if ( executeCount==1 ) {
							lastExecute = j;
						}
						if ( executeCount>=3 ) {
							break;
						}
					}
				}
				notExecuteList.get(i).put("sumExecute", executeCount);
				if ( executeCount==3 ) {
					notExecuteList.remove(i);
					i--;
				}else if ( executeCount==0 ) {
					notExecuteList.get(i).put("lastTime", "未执行");
				}else {
					notExecuteList.get(i).put("lastTime", todayList.get(lastExecute).get("SUBMITTIME"));
				}
			}
			
			//每天4次   6小时一次
			else if ( freqId.equals("qid") || freqId.equals("q6h") ) {
				int executeCount = 0;
				int lastExecute = 0;
				for (int j = 0; j < todayList.size(); j++) {
					String todayOrdId = todayList.get(j).get("ORDID").toString();
					if ( todayOrdId.equals(notOrdId) ) {
						executeCount++;
						if ( executeCount==1 ) {
							lastExecute = j;
						}
						if ( executeCount>=4 ) {
							break;
						}
					}
				}
				notExecuteList.get(i).put("sumExecute", executeCount);
				if ( executeCount==4 ) {
					notExecuteList.remove(i);
					i--;
				}else if ( executeCount==0 ) {
					notExecuteList.get(i).put("lastTime", "未执行");
				}else {
					notExecuteList.get(i).put("lastTime", todayList.get(lastExecute).get("SUBMITTIME"));
				}
			}
			
			//一日五次
			else if ( freqId.equals("FID") ) {
				int executeCount = 0;
				int lastExecute = 0;
				for (int j = 0; j < todayList.size(); j++) {
					String todayOrdId = todayList.get(j).get("ORDID").toString();
					if ( todayOrdId.equals(notOrdId) ) {
						executeCount++;
						if ( executeCount==1 ) {
							lastExecute = j;
						}
						if ( executeCount>=5 ) {
							break;
						}
					}
				}
				notExecuteList.get(i).put("sumExecute", executeCount);
				if ( executeCount==5 ) {
					notExecuteList.remove(i);
					i--;
				}else if ( executeCount==0 ) {
					notExecuteList.get(i).put("lastTime", "未执行");
				}else {
					notExecuteList.get(i).put("lastTime", todayList.get(lastExecute).get("SUBMITTIME"));
				}
			}
			
			//  4小时一次  //  3小时一次  //  2小时一次  //  1小时一次   //  0.5小时一次
			else if ( freqId.equals("q4h") || freqId.equals("q3h") || freqId.equals("q0.5h") ||
					freqId.equals("q2h") || freqId.equals("qh") ) {
				int executeCount = 0;
				int lastExecute = 0;
				for (int j = 0; j < todayList.size(); j++) {
					String todayOrdId = todayList.get(j).get("ORDID").toString();
					if ( todayOrdId.equals(notOrdId) ) {
						executeCount++;
						if ( executeCount==1 ) {
							lastExecute = j;
						}
					}
				}
				notExecuteList.get(i).put("sumExecute", executeCount);
				if ( executeCount==0 ) {
					notExecuteList.get(i).put("lastTime", "未执行");
				}else {
					notExecuteList.get(i).put("lastTime", todayList.get(lastExecute).get("SUBMITTIME"));
				}
			}
			
			//  两天一次
			else if ( freqId.equals("qod") ) {
				String temSql = "select * from DRUGPLAN dd where dd.INPATIID='" + inpatiId 
						+ "' AND SUBMITTIME > trunc(sysdate) - 1 AND SUBMITTIME <= trunc(sysdate) + 1 ORDER BY SUBMITTIME DESC";
				List<Map<String, Object>> temList = jdbcTemplate.queryForList(temSql);
				for (int j = 0; j < temList.size(); j++) {
					String temOrdId = temList.get(j).get("ORDID").toString();
					if ( temOrdId.equals(notOrdId) ) {
						notExecuteList.remove(i);
						i--;
						break;
					}
				}
			}
			
			//  三天一次
			else if ( freqId.equals("Q3D") ) {
				String temSql = "select * from DRUGPLAN dd where dd.INPATIID='" + inpatiId 
						+ "' AND SUBMITTIME > trunc(sysdate) - 2 AND SUBMITTIME <= trunc(sysdate) + 1 ORDER BY SUBMITTIME DESC";
				List<Map<String, Object>> temList = jdbcTemplate.queryForList(temSql);
				for (int j = 0; j < temList.size(); j++) {
					String temOrdId = temList.get(j).get("ORDID").toString();
					if ( temOrdId.equals(notOrdId) ) {
						notExecuteList.remove(i);
						i--;
						break;
					}
				}
			}
			
			//  一周一次
			else if ( freqId.equals("qw") || freqId.equals("q w") ) {
				for (int j = 0; j < weekList.size(); j++) {
					String temOrdId = weekList.get(j).get("ORDID").toString();
					if ( temOrdId.equals(notOrdId) ) {
						notExecuteList.remove(i);
						i--;
						break;
					}
				}
			}
			
			//  一周两次
			else if ( freqId.equals("biw") ) {
				int executeCount = 0;
				int lastExecute = 0;
				for (int j = 0; j < weekList.size(); j++) {
					String todayOrdId = weekList.get(j).get("ORDID").toString();
					if ( todayOrdId.equals(notOrdId) ) {
						executeCount++;
						if ( executeCount==1 ) {
							lastExecute = j;
						}
						if ( executeCount>=2 ) {
							break;
						}
					}
				}
				notExecuteList.get(i).put("sumExecute", executeCount);
				if ( executeCount==2 ) {
					notExecuteList.remove(i);
					i--;
				}else if ( executeCount==0 ) {
					notExecuteList.get(i).put("lastTime", "未执行");
				}else {
					notExecuteList.get(i).put("lastTime", todayList.get(lastExecute).get("SUBMITTIME"));
				}
			}
			
			//  一周三次
			else if ( freqId.equals("tiw") ) {
				int executeCount = 0;
				int lastExecute = 0;
				for (int j = 0; j < weekList.size(); j++) {
					String todayOrdId = weekList.get(j).get("ORDID").toString();
					if ( todayOrdId.equals(notOrdId) ) {
						executeCount++;
						if ( executeCount==1 ) {
							lastExecute = j;
						}
						if ( executeCount>=3 ) {
							break;
						}
					}
				}
				notExecuteList.get(i).put("sumExecute", executeCount);
				if ( executeCount==3 ) {
					notExecuteList.remove(i);
					i--;
				}else if ( executeCount==0 ) {
					notExecuteList.get(i).put("lastTime", "未执行");
				}else {
					notExecuteList.get(i).put("lastTime", todayList.get(lastExecute).get("SUBMITTIME"));
				}
			}
		}
		for (int i = 0; i < notExecuteList.size(); i++) {//时间格式化
			if ( notExecuteList.get(i).get("sumExecute").toString().equals("0") ) {
				
			}else {//执行次数 不为1
				notExecuteList.get(i).put("lastTime", time.format((Date)notExecuteList.get(i).get("lastTime")));
			}
		}
		return notExecuteList;
	}
	
	@Override
	public List<Map<String, Object>> getDrugPlanListFromTabDrugPlan(
			String inpatiId) {
		if ( StringUtils.isEmpty(inpatiId) ) {
			return null;
		}
		String sql = "select * from drugplan dd where dd.INPATIID='" + inpatiId + "'";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
	
		for (int i = 0; i < list.size(); i++) {//时间格式化
			list.get(i).put("SUBMITTIME", time.format((Date)list.get(i).get("SUBMITTIME")));
		}
		return list;
	} 

	@Override
	public Boolean executeOrderByNurseLoginId(String orderId,
			String nurseLoginId) {
		if ( StringUtils.isEmpty(orderId) || StringUtils.isEmpty(nurseLoginId) ) {
			return false;
		}
		int result =0;
		DataSource dataSource = jdbcTemplate.getDataSource();  
		Connection conn=null;  
		try {
			conn=dataSource.getConnection();
			CallableStatement cs = conn.prepareCall("{call DRUGPLAN_ADD(?,?,?)}");   
			cs.setString(1, orderId);  
			cs.setString(2, nurseLoginId); 
			cs.registerOutParameter(3, OracleTypes.NUMBER);
			cs.execute();  
			result = cs.getInt(3);//获取单个返回值
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}  
		if ( result>0 ) {
			return true;
		}else {
			return false;
		}
	}
	
	@Override
	public List<Map<String, Object>> getDrugInfoByOrderid(String orderId) {
		if ( StringUtils.isEmpty(orderId) ) {
			return null;
		}
		String sql = "select * from view_ehis_pqzx "
				+ "where ORDID='" + orderId + "'";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}

	@Override
	public List<Map<String, Object>> getDrugInfoByNurseId(String loginName) {
		if ( StringUtils.isEmpty(loginName) ) {
			return null;
		}
		String sql = "select * from view_ehis_pqzx "
				+ "where NURSEEMPID LIKE '%" + loginName + "%'";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}

}
